/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.bjj.utils;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

/**
 *
 * @author bill
 */
public class ManifestReader {

    public static Map<String, Attributes> readManifestEntries(String file) throws IOException {
        return readManifestEntries(new File(file));
    }

    public static Map<String, Attributes> readManifestEntries(File file) throws IOException {

        if (!file.canRead()) {
            System.err.println(file.getAbsolutePath() + ": no such file");
        }
        
            JarFile jar = new JarFile(file);
            final Manifest manifest = jar.getManifest();
            return manifest.getEntries();
//            final Map<String, Attributes> attrs = manifest.getEntries();
//            for (String name : attrs.keySet()) {
//                final Attributes attr = attrs.get(name);
//                System.out.println(name + ": \n");
//                for (Object a : attr.keySet()) {
//                    System.out.println("\t " + a + ": " + attr.getValue((Name) a));
//                }
//            }
    }

    public static Attributes readManifestMainAttributes(String file) throws IOException {
        return readManifestMainAttributes(new File(file));
    }

    public static Attributes readManifestMainAttributes(File file) throws IOException {
        if (!file.canRead()) {
            System.err.println(file.getAbsolutePath() + ": no such file");
        }

            JarFile jar = new JarFile(file);
            final Manifest manifest = jar.getManifest();
            return manifest.getMainAttributes();
//            System.out.println(file.getAbsolutePath());
//            System.out.println("Main attrs: ");
//            for (Object a : mattr.keySet()) {
//                Name name = (Name)a;
//
//                name.toString();
//                System.out.println("\t " + a + ": " + mattr.getValue((Name) a));
//            }
    }
}